package com.opal.system.service.impl;

import com.opal.config.shiro.ShiroPermissionFactory;
import com.opal.domain.ReturnMessageData;
import com.opal.system.entity.table.*;
import com.opal.system.mapper.mysql.*;
import com.opal.system.service.ManageService;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class ManageServiceImpl implements ManageService {
    @Autowired
    private OpalPermissionMapper permissionMapper;
    @Autowired
    private OpalRoleMapper opalRoleMapper;
    @Autowired
    private OpalUserMapper opalUserMapper;
    @Autowired
    private OpalMenuMapper opalMenuMapper;
    @Autowired
    private OpalUserRoleMapper opalRoleMenuMapper;

	@Autowired
	private ShiroPermissionFactory shiroPermissionFactory;
	@Autowired
	private ShiroFilterFactoryBean shiroFilterFactoryBean;

    @Override
    @Transactional("mysqlTransactionManager")
    public ReturnMessageData addPermissionSercice(OpalPermission permission) {
        ReturnMessageData rmd = null;

        if (permission != null) {
            try {
                int i = permissionMapper.insertSelective(permission);
                if (i > 0) {
                    rmd = new ReturnMessageData(200, "增加权限成功");
                } else {
                    rmd = new ReturnMessageData(500, "增加权限失败");
                }
                //动态重新配置权限
                shiroPermissionFactory.updatePermission(shiroFilterFactoryBean);
            } catch (Exception e) {
            	e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                rmd = new ReturnMessageData(500, "数据库维护中，请稍候再试");
            }
        }
        return rmd;
    }

    @Override
	@Transactional("mysqlTransactionManager")
    public ReturnMessageData updatePermissionSercice(OpalPermission permission) {
        ReturnMessageData rmd = null;

        if (permission != null) {
            try {
                int i = permissionMapper.updateByPrimaryKeySelective(permission);
                if (i > 0) {
                    rmd = new ReturnMessageData(200, "修改权限成功");
                } else {
                    rmd = new ReturnMessageData(500, "修改权限失败");
                }
                //动态重新配置权限
				shiroPermissionFactory.updatePermission(shiroFilterFactoryBean);
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                rmd = new ReturnMessageData(500, "数据库维护中，请稍候再试");
            }
        }

        return rmd;
    }

    @Override
    public List<OpalPermission> selectAllPermissionSercice() {
        OpalPermissionExample ope = new OpalPermissionExample();
        List<OpalPermission> selectByExample = permissionMapper.selectByExample(ope);
        return selectByExample;
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public ReturnMessageData deletePermissionSercice(OpalPermission[] opalPermissions) {
        ReturnMessageData rmd = null;
        try {
            for (OpalPermission o : opalPermissions) {
                //删除权限
                Long permissionId = o.getPermissionId();
                int restlt = permissionMapper.deleteByPrimaryKey(permissionId);

            }
            //动态重新配置权限
            shiroPermissionFactory.updatePermission(shiroFilterFactoryBean);
            rmd = new ReturnMessageData(200, "删除权限成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rmd = new ReturnMessageData(500, "删除失败，请尝试重新操作或联系管理员");
        }
        return rmd;
    }


}
